<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Member;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 店铺收益管理
 *
 * @icon fa fa-circle-o
 */
class Revenue extends Backend
{

    /**
     * Member模型对象
     * @var \app\common\model\Member
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Washlog();
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typedataList", $this->model->getTypeDataList());
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->alias("a")
            ->field('a.id,a.name,merchantuser_money,a.createtime,b.nickname,b.mobile,c.name merchant_name,c.address,1 type_data')
            ->join("member b","a.member_id=b.id","left")
            ->join("merchant c","a.merchant_id=c.id","left")
            ->union("SELECT a.id,a.name,merchantuser_money,a.createtime,b.nickname,b.mobile,c.name merchant_name,c.address,2 type_data  FROM `tx_order` a LEFT JOIN tx_member b ON a.member_id=b.id LEFT JOIN tx_merchant c ON a.merchant_id=c.id where  (type_data ='0' or type_data='2') and (a.status='1' or a.status='3')")
            ->order("createtime","desc")
            ->limit($offset,$limit)
            ->select();
        foreach ($list as $v){
            $model=new Member();
            $v["nickname"]=$model->emoji_decode($v["nickname"]);
        }
        $total = Db::query("SELECT COUNT(*) count FROM (
SELECT a.id   FROM `tx_washlog` a LEFT JOIN tx_member b ON a.member_id=b.id LEFT JOIN tx_merchant c ON a.merchant_id=c.id
UNION ALL
SELECT a.id   FROM `tx_order` a LEFT JOIN tx_member b ON a.member_id=b.id LEFT JOIN tx_merchant c ON a.merchant_id=c.id where  (type_data ='0' or type_data='2') and (a.status='1' or a.status='3')
) num");
        $result = ['total' => $total[0]["count"], 'rows' => $list];
        return json($result);
    }

}
